package com.zrrd.yt.list.controller;

import com.zrrd.yt.common.result.Result;
import com.zrrd.yt.list.service.SearchService;
import com.zrrd.yt.model.list.Goods;
import com.zrrd.yt.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    //操作es的对象
    @Autowired
    private ElasticsearchRestTemplate template;
    @Autowired
    private SearchService searchService;

    //创建索引库
    @GetMapping("/createIndex")
    public Result createIndex() {
        //创建索引库
        template.createIndex(Goods.class);
        //加载映射结构
        template.putMapping(Goods.class);
        return Result.ok();
    }

    //上架商品
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }
    //下架商品
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId){
        searchService.lowerGoopds(skuId);
        return Result.ok();
    }

    //更新热度
    @GetMapping("/inner/updateHotScore/{skuId}")
    public Result updateHotScore(@PathVariable("skuId") Long skuId){
        searchService.updateHotScore(skuId);
        return Result.ok();
    }

    //搜索商品
    @PostMapping
    public Result search(@RequestBody SearchParam param) throws IOException {
        return Result.ok(searchService.search(param));
    }

}
